package b1w1nn1ng.crossf1t.business;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

import b1w1nn1ng.crossf1t.db.Database;

/** 
 * Used for sorting and displaying a list of friends
 * 
 * @author Chris
 *
 */
public class Friend implements Comparable<Friend> {
	private int ID;
	private User user;

	private Friend(Integer inID) {
		//Testing stub:
//		 ID = inID;
//		User test = new User();
//		test.setFirstName("This is my first name!");
//		test.setLastName("pingpong");
//		test.setId(11111);
//		user = test; 
		
		ID = inID;
		
		try {
			user = Database.getUserByID(inID);
		} catch (SQLException e) {
			// Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Used in the getSortedList method below
	 */
	@Override
	public int compareTo(Friend other) {
		return this.getUser().getLastName().compareTo(other.getUser().getLastName());
	}	
	
	/**
	 * Creates and returns all of the user's friends, sorted by name. This is basically
	 * the only reason this class exists. Friend objects contain the name for user display
	 * and the ID, so that we can get them from the database if the user wants to see
	 * that friend's profile. The database will be hit for each of the user's friends once,
	 * which could take a while and be slow, but it's the only way we can display them by
	 * name without creating a local list of names.
	 * 
	 * @param idList List of Integers representing the user's friends' IDs.
	 * @return A list of friend objects representing the user's friends, containing their
	 * names and IDs.
	 */
	public static ArrayList<Friend> getSortedList(ArrayList<Integer> idList) {
		ArrayList<Friend> friendList = new ArrayList<Friend>();
		System.out.println("idList in Friend.java" + idList);
		for(Integer i : idList) {
			friendList.add(new Friend(i));
		}
		Collections.sort(friendList);
		return friendList;
	}
	
	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
